package com.vcredit.creditcat.service.product.xyh;

import java.math.BigDecimal;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.service.CustProductApplyService;
import com.vcredit.creditcat.service.product.IProductApplyResultService;
import com.vcredit.creditcat.service.product.shared.ProductApplyServiceAdaptor;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.creditcat.vo.product.xyh.QueryParamVo;
import com.vcredit.creditcat.vo.product.xyh.QueryResultVo;
import com.vcredit.creditcat.vo.product.xyh.ResultVo;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service(ProductApplyServiceAdaptor.RESULT_BEANNAME_PREFIX+6)
public class XYHApplyResultServiceImpl implements IProductApplyResultService{
	private  Logger logger = LoggerFactory.getLogger(XYHApplyResultServiceImpl.class);
	
	@Value("${product.xyh.queryUrl}")
	private String queryUrl;
	
	private static final Integer TIMEOUT = 60; // 请求超时时间60s
	
	@Autowired
	private CustProductApplyService custProductApplyService;
	
	
	@Log
	public CustProductApply getProductApplyResult(Cust cust,long productId) throws Exception{
								
			QueryParamVo queryParam = new QueryParamVo(cust.getCustIdno(), cust.getCustName(),cust.getCustMobile());
			String queryParamStr = JacksonUtil.Obj2Json(queryParam);
			logger.info("信用花查询状态入参："+queryParamStr);
			String queryResultStr = HttpRequest.doJson(queryUrl, queryParamStr, TIMEOUT);
			logger.info("信用花查询状态出参："+queryResultStr);
			ResultVo<QueryResultVo> result = JacksonUtil.Json2Obj(queryResultStr, new TypeReference<ResultVo<QueryResultVo>>(){});
			CustProductApply custProductApply = new CustProductApply();
			
			if(result.getStatus().equals("000")) {
				if(result.getData()!=null){	
					QueryResultVo content = result.getData();
					// 处理黑名单、灰名单
					if("1".equals(content.getListType())) {
						custProductApply.setAuditResult(0);
						custProductApply.setRejectMsg("信用花黑名单");
						custProductApply.setIsApply(false);
						return custProductApply;
					} else if("2".equals(content.getListType())) {
						custProductApply.setAuditResult(0);
						custProductApply.setRejectMsg("信用花灰名单");
						custProductApply.setIsApply(true);
						custProductApply.setCreditApplyTime(content.getListExpireDate() == null ? null : ConcurrentDateUtil.formatDateTime(content.getListExpireDate()));
						return custProductApply;
					}					
					// 处理授信状态
					String creditStatus = content.getCreditStatus();
					if(creditStatus==null){
						return custProductApply;
					}
					switch (creditStatus) {
					case "2": // 拒绝授信
						custProductApply.setAuditResult(0);
						custProductApply.setRejectMsg("信用花拒绝授信");
						custProductApply.setIsApply(true);
						custProductApply.setCreditApplyTime(ConcurrentDateUtil.formatDateTime(ConcurrentDateUtil.addDayTime(new Date(),30)));
						return custProductApply;
					case "4": // 已完成授信
						custProductApply.setAuditResult(1);
						custProductApply.setIsApply(false);
						custProductApply.setCreditAmt(content.getTotalAccount() == null ? 0 : (int)content.getTotalAccount().doubleValue());
						custProductApply.setCreditExpiretime(content.getAccountExpireDate() == null ? null : ConcurrentDateUtil.formatDateTime(content.getAccountExpireDate()));
						custProductApply.setServiceRate(content.getMonthPlatServiceRate() == null ? null : new BigDecimal(content.getMonthPlatServiceRate()));
						custProductApply.setTotalServiceRate(content.getMonthComprehRate() == null ? null : new BigDecimal(content.getMonthComprehRate()));
						return custProductApply;					
						
					case "7": // 信用补救，可重新授信
						custProductApply.setAuditResult(7);
						custProductApply.setRejectMsg("信用花信用补救");
						custProductApply.setIsApply(false);
						return custProductApply;
					
					}
				}
			}
			return custProductApply;
		}
}


